Skip to content

perf(sqlite-native): reuse read cache for partial writes#4632

Open
NathanFlurry wants to merge 1 commit into04-12-perf_sqlite-native_remove_delete_file_existence_probefrom
04-12-perf_sqlite-native_reuse_read_cache_for_partial_writes
Open

perf(sqlite-native): reuse read cache for partial writes#4632
NathanFlurry wants to merge 1 commit into04-12-perf_sqlite-native_remove_delete_file_existence_probefrom
04-12-perf_sqlite-native_reuse_read_cache_for_partial_writes

Conversation

@NathanFlurry
Copy link
Copy Markdown
Member

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copy link
Copy Markdown
Member Author

NathanFlurry commented Apr 13, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@claude
Copy link
Copy Markdown

claude bot commented Apr 13, 2026

PR Review: perf(sqlite-native): reuse read cache for partial writes

Overview

This PR optimizes the native SQLite VFS write path by consulting the read cache before issuing a KV round-trip for partial-chunk writes. When needs_existing is true and read_cache_enabled is set, the write planner now checks state.read_cache first and only adds the chunk to the KV fetch list on a cache miss. The approach is correct in principle — the read cache is already kept consistent (populated on reads, updated on writes, cleared on truncate) — so reusing it for writes avoids redundant KV round-trips.


Bug: Likely Compile Error

The cache lookup calls .get() directly on Option<HashMap<Vec<u8>, Vec<u8>>>:

let cached_chunk = if needs_existing && ctx.read_cache_enabled {
    let state = get_file_state(file.state);
    state.read_cache.get(chunk_key.as_slice()).cloned()  // compile error
} else {
    None
};

Option<T> has no get(key) method in Rust. The consistent pattern used everywhere else in this file (e.g. lines 431–434, 448–452, 672, 728) is to unwrap the Option first:

// Existing pattern throughout the file:
if let Some(read_cache) = state.read_cache.as_ref() {
    read_cache.get(key.as_slice())
}

// Correct form for the new one-liner:
state.read_cache.as_ref().and_then(|c| c.get(chunk_key.as_slice())).cloned()

The ctx.read_cache_enabled guard ensures the cache is Some at runtime, but Rust's type checker does not know that — this should fail to compile.


Pre-existing Concern: Cache Updated Before KV Write Succeeds

Lines 672–679 insert the new chunk data into the read cache before the KV put on line 683. If the KV write fails, the file size is rolled back (lines 684–686) but the cache retains the not-yet-persisted data. A subsequent partial write to the same chunk will now use the stale cached bytes as the "existing" base, potentially producing a corrupted page.

This is a pre-existing issue, but this PR extends its surface: previously, stale cache entries only affected reads; now they also gate whether KV is consulted during writes.

A simple fix: update the cache after confirming the KV put succeeded, or evict affected keys on write failure.


WASM VFS Parity

CLAUDE.md requires that the native Rust VFS and the WASM TypeScript VFS match 1:1. The WASM VFS (packages/sqlite-wasm/src/vfs.ts) does not appear to have the same read-cache infrastructure, so a direct port may not apply. However, the PR should explicitly note this gap and confirm that the behavioral contract is unchanged (same KV reads/writes, only the fetch elision differs).


Test Coverage

No tests are added for the cache-hit path during partial writes. A regression test asserting that KV fetch count is reduced when a chunk is already in the read cache would be valuable here, especially given the cache/KV ordering issue noted above.


Summary

Item Status
Optimization logic Sound when it compiles
Compile error (Option::get()) Bug — fix to .as_ref().and_then(...)
Cache/KV ordering on write failure Pre-existing issue, amplified by this PR
WASM VFS parity Should be explicitly documented as N/A or addressed
Test coverage No new coverage for the cache-hit write path

@NathanFlurry NathanFlurry marked this pull request as ready for review April 13, 2026 05:13
@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_remove_delete_file_existence_probe branch from 87e4122 to 8c30d87 Compare April 13, 2026 05:38
@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_reuse_read_cache_for_partial_writes branch from d81f1b5 to 18d65ab Compare April 13, 2026 05:38
@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_remove_delete_file_existence_probe branch from 8c30d87 to 86fec2b Compare April 13, 2026 05:50
@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_reuse_read_cache_for_partial_writes branch 2 times, most recently from 349925c to c7ac8f9 Compare April 13, 2026 07:03
@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_remove_delete_file_existence_probe branch from 86fec2b to 79dfb24 Compare April 13, 2026 07:03
@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_remove_delete_file_existence_probe branch from 79dfb24 to b3ff6d3 Compare April 13, 2026 21:07
@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_reuse_read_cache_for_partial_writes branch from c7ac8f9 to 28c85fd Compare April 13, 2026 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant